package com.jrx.anytxn.param.service.investor.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.investor.PrInvestorPrepaymentFeeReq;
import com.jrx.anytxn.param.dto.investor.PrInvestorPrepaymentFeeRes;
import com.jrx.anytxn.param.entity.PrInvestorPrepaymentFee;
import com.jrx.anytxn.param.mapper.ext.ExtPrInvestorPrepaymentFeeMapper;
import com.jrx.anytxn.param.service.investor.IPrInvestorPrepaymentFeeService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 出资方提前还款手续费表 业务接口实现
 *
 * @author 黄秋平
 * @date 2020年5月19日11:41:49
 */
@Service
@CacheConfig(cacheNames = "INVESTOR_PREPAYMENT")
public class PrInvestorPrepaymentFeeServiceImpl implements IPrInvestorPrepaymentFeeService {
    private static final Logger logger = LoggerFactory.getLogger(PrInvestorPrepaymentFeeServiceImpl.class);

    @Resource
    private ExtPrInvestorPrepaymentFeeMapper extPrInvestorPrepaymentFeeMapper;

    @Resource
    private ISegmentService segmentService;


    @Override
    public TxnPage<PrInvestorPrepaymentFeeRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrInvestorPrepaymentFee> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrInvestorPrepaymentFee> prPrepaymentFeeList = extPrInvestorPrepaymentFeeMapper.selectByTenId(tenantId);
        if (prPrepaymentFeeList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrInvestorPrepaymentFeeRes> res = BeanMapping.copyList(prPrepaymentFeeList, PrInvestorPrepaymentFeeRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrInvestorPrepaymentFeeRes addPrPrepaymentFee(PrInvestorPrepaymentFeeReq prPrepaymentFeeReq) throws TxnException {
        if (prPrepaymentFeeReq.getMaxAmount() != null && prPrepaymentFeeReq.getMinAmount() != null && prPrepaymentFeeReq.getMaxAmount().compareTo(prPrepaymentFeeReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrInvestorPrepaymentFee prPrepaymentFee = extPrInvestorPrepaymentFeeMapper.selectByPrepaymentIdAndOrganizationId(prPrepaymentFeeReq.getInvestorPrepaymentFeeId(), prPrepaymentFeeReq.getOrganizationId(), prPrepaymentFeeReq.getTenantId());
        if (prPrepaymentFee != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrInvestorPrepaymentFee prepaymentFee = BeanMapping.copy(prPrepaymentFeeReq, PrInvestorPrepaymentFee.class);
        try {
            prepaymentFee.setId(segmentService.getId(Constant.PR_INVESTOR_PREPAYMENT_FRR));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        prepaymentFee.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrInvestorPrepaymentFeeMapper.insertSelective(prepaymentFee);
        return BeanMapping.copy(prepaymentFee, PrInvestorPrepaymentFeeRes.class);
    }

    @Override
    @CacheEvict(key = "'investorPrepayment_'+#prPrepaymentFeeReq.tenantId+'_'+#prPrepaymentFeeReq.investorPrepaymentFeeId")
    public PrInvestorPrepaymentFeeRes modifyPrPrepaymentFee(PrInvestorPrepaymentFeeReq prPrepaymentFeeReq) throws TxnException {
        if (prPrepaymentFeeReq.getMaxAmount() != null && prPrepaymentFeeReq.getMinAmount() != null && prPrepaymentFeeReq.getMaxAmount().compareTo(prPrepaymentFeeReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrInvestorPrepaymentFee prPrepaymentFee = extPrInvestorPrepaymentFeeMapper.selectByTenIdAndFeeId(prPrepaymentFeeReq.getTenantId(),prPrepaymentFeeReq.getInvestorPrepaymentFeeId());
        if (prPrepaymentFee == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrInvestorPrepaymentFee prepaymentFee = BeanMapping.copy(prPrepaymentFeeReq, PrInvestorPrepaymentFee.class);
        // 更新
        extPrInvestorPrepaymentFeeMapper.updateByTenIdAndFeeId(prepaymentFee);
        return BeanMapping.copy(prepaymentFee, PrInvestorPrepaymentFeeRes.class);

    }

    @Override
    @CacheEvict(key = "'investorPrepayment_'+#tenantId+'_'+#investorPrepaymentFeeId")
    public Boolean removeByTenIdAndFeeId(String tenantId, String investorPrepaymentFeeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorPrepaymentFeeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrInvestorPrepaymentFeeMapper.deleteByTenIdAndFeeId(tenantId, investorPrepaymentFeeId);
        return i > 0;
    }

    @Override
    public PrInvestorPrepaymentFeeRes findByTenIdAndFeeId(String tenantId, String investorPrepaymentFeeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorPrepaymentFeeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrInvestorPrepaymentFee prepaymentFee = extPrInvestorPrepaymentFeeMapper.selectByTenIdAndFeeId(tenantId, investorPrepaymentFeeId);
        return BeanMapping.copy(prepaymentFee, PrInvestorPrepaymentFeeRes.class);
    }


    @Cacheable(key = "'investorPrepayment_'+#tenantId+'_'+#investorPrepaymentFeeId")
    @Override
    public PrInvestorPrepaymentFee findByPrepaymentFeeId(String investorPrepaymentFeeId,  String tenantId) throws TxnBizException {
        PrInvestorPrepaymentFee prepaymentFee = extPrInvestorPrepaymentFeeMapper.selectByTenIdAndFeeId(tenantId,investorPrepaymentFeeId);
        if (prepaymentFee == null) {
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), TxnRespCode.NO_DATA.getMsg(ErrorMsgConstant.PREPAYMENT_FEE));
        }
        return prepaymentFee;
    }
}
